package SM.UI.Controls;

import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DropDown extends Widget{
	
	/*Variable Declaration*/
	private List<String> ID = new ArrayList<String>();
	private List<String> Text = new ArrayList<String>();
	
	private int IdIndex;
	private int TextIndex;
	
	private int SelectedOptionIndex;
	private String SelectedOptionValue;
	
	/*Constructor*/
	
	public DropDown()
	{
		super();
	}
	
	public DropDown(String name, int width)
	{
		super(name, width);
	}
	
	public DropDown(String name, int width, String style)
	{
		super(name,width,style);
	}
	/*Getter-Setter*/
	
	/*public int GetWidth()
	{
		return this.Width;
	}
	
	public void SetWidth(int width)
	{
		this.Width = width;
	}
	
	public void SetName(String name)
	{
		this.Name = name;
	}
	
	public void SetStyle(String style)
	{
		this.Style = style;
	}
	*/
	public void SetDataSource(ResultSet dataSource)
	{
		super.SetDataSource(dataSource);
		this.IdIndex = 0;
		this.TextIndex = 1;
	}
	
	public void SetDataSource(ResultSet dataSource, int idIndex, int textIndex)
	{
		super.SetDataSource(dataSource);
		this.IdIndex = idIndex;
		this.TextIndex = textIndex;
	}
	
	public void SetSelectedOption(int option)
	{
		this.SelectedOptionIndex = option;
	}
	
	public int GetSelectedOptionIndex()
	{
		return this.SelectedOptionIndex;
	}
	
	public void SetSelectedOption(String option)
	{
		this.SelectedOptionValue = option;
	}
	
	public String GetSelectedOptionValue()
	{
		return this.SelectedOptionValue;
	}
	/*Functions*/
	
	public void DataBind()
	{
		//ResultSet resultSet = this.DataSource;
		ResultSet resultSet = super.GetDataSource();
		
		try {
			
				while(resultSet.next())
				{
					this.ID.add(resultSet.getString(this.IdIndex));
					this.Text.add(resultSet.getString(this.TextIndex));
				}
			
		}catch(SQLException se) { System.out.println("Oops! We think ResultSet is empty or some problem occured " 
		+ "in DataBind()"); this.ID.clear(); this.Text.clear();}
	}
	
	public String toString()
	{
		String Class = (super.GetStyle() == "") ? "default" : super.GetStyle();
		
		String DisplayString = "<select name=\"" + super.GetName() +  "\" style=\"width:" + super.GetWidth() + "px;\" width='" + super.GetWidth() + "px' class='" + Class + "'>\n";
		
		for(int Index=0; Index < this.ID.size(); Index++)
		{
			if(Index == this.SelectedOptionIndex || this.ID.get(Index).equals(this.SelectedOptionValue) ||
					this.Text.get(Index).equals(this.SelectedOptionValue))
				DisplayString += "<option value=\"" + this.ID.get(Index) +"\" selected>" + this.Text.get(Index) + " </option>\n";
			else
				DisplayString += "<option value=\"" + this.ID.get(Index) +"\">" + this.Text.get(Index) + " </option>\n";
		}
		
		DisplayString += "</select>\n";
		
		return DisplayString;
	}
	
	public String getHTML()
	{
		return this.toString();
	}

}
